package View;


import Config.GlobalVariables;

import DAO.ProductsDAO;
import Entity.Products;
import Entity.Robot;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import Entity.RobotStatus;
import Service.SupervisorService;
import Service.utils.ShortestPath;

import static Config.GlobalVariables.gridList;

public class Warehouse extends JFrame {
    private final Image arrowIcon = scaleImage("/View/images/arrow-24.png", 24, 24);
    private final Image chargingIcon = scaleImage("/View/images/plug-24.png", 24, 24);
    private Image robotAOIcon = scaleImage("/View/images/a-orange.png", 24, 24);
    private Image robotARIcon = scaleImage("/View/images/a-red.png", 24, 24);
    private Image robotAGIcon = scaleImage("/View/images/a-green.png", 24, 24);
    private Image robotBOIcon = scaleImage("/View/images/b-orange.png", 24, 24);
    private Image robotBRIcon = scaleImage("/View/images/b-red.png", 24, 24);
    private Image robotBGIcon = scaleImage("/View/images/b-green.png", 24, 24);
    private Image robotCOIcon = scaleImage("/View/images/c-orange.png", 24, 24);
    private Image robotCRIcon = scaleImage("/View/images/c-red.png", 24, 24);
    private Image robotCGIcon = scaleImage("/View/images/c-green.png", 24, 24);

    private Image robotAOcaryingIcon = scaleImage("/View/images/a-orange-black.png", 24, 24);
//    private Image robotARcaryingIcon = scaleImage("/View/images/a-red-black.png", 24, 24);
    private Image robotAGcaryingIcon = scaleImage("/View/images/a-green-black.png", 24, 24);
    private Image robotBOcaryingIcon = scaleImage("/View/images/b-orange-black.png", 24, 24);
//    private Image robotBRcaryingIcon = scaleImage("/View/images/b-red-black.png", 24, 24);
    private Image robotBGcaryingIcon = scaleImage("/View/images/b-green-black.png", 24, 24);
    private Image robotCOcaryingIcon = scaleImage("/View/images/c-orange-black.png", 24, 24);
//    private Image robotCRcaryingIcon = scaleImage("/View/images/c-red-black.png", 24, 24);
    private Image robotCGcaryingIcon = scaleImage("/View/images/c-green-black.png", 24, 24);
    private Robot robotA;
    private Robot robotB;
    private Robot robotC;

    public Warehouse(java.util.List<Robot> allRobots) {
        setTitle("Robot Grid with Moving Robots");
        this.robotA = allRobots.get(0);
        this.robotB = allRobots.get(1);
        this.robotC = allRobots.get(2);
       // robotBIcon = scaleImage(robotB.getIconPath(), 24, 24);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(GlobalVariables.GRID_COLUMNS * GlobalVariables.CELL_SIZE + 200,
                GlobalVariables.GRID_ROWS * GlobalVariables.CELL_SIZE + 200);
        setLocationRelativeTo(null);
        java.util.List<int[]> path1 = new ArrayList<>();
        ShortestPath shortestPath = new ShortestPath(gridList);
        java.util.List<int[]> path = new ArrayList<>();
        add(new GridPanel());

        Timer timer = new Timer(10, e -> {
            repaint();
        });
        timer.start();
    }

    private class GridPanel extends JPanel {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // 绘制网格和内容
            for (int row = 0; row < GlobalVariables.GRID_ROWS; row++) {
                for (int col = 0; col < GlobalVariables.GRID_COLUMNS; col++) {
                    int x = col * GlobalVariables.CELL_SIZE + 100;
                    int y = row * GlobalVariables.CELL_SIZE + 100;

                    // 绘制单元格边框
                    g2d.setColor(Color.LIGHT_GRAY);
                    g2d.drawRect(x, y, GlobalVariables.CELL_SIZE, GlobalVariables.CELL_SIZE);

                    // 根据 gridList 的编号绘制内容
                    int cellValue = gridList.get(row).get(col);
                    switch (cellValue) {
                        case 1: // 书架格子
                            g2d.setColor(new Color(64, 64, 64, 128)); // 深灰色高透明度
                            g2d.fillRect(x + 1, y + 1, GlobalVariables.CELL_SIZE - 2, GlobalVariables.CELL_SIZE - 2);
                            break;
                        case 2:
                            g2d.setColor(new Color(16, 16, 64, 128)); // 深灰色高透明度
                            g2d.fillRect(x + 1, y + 1, GlobalVariables.CELL_SIZE - 2, GlobalVariables.CELL_SIZE - 2);
                            break;
                        case 3: // 左边箭头
                            g2d.drawImage(arrowIcon, x + (GlobalVariables.CELL_SIZE - 24) / 2,
                                    y + (GlobalVariables.CELL_SIZE - 24) / 2, null);
                            break;
                        case 4: // 右边箭头
                            g2d.drawImage(arrowIcon, x + (GlobalVariables.CELL_SIZE - 24) / 2,
                                    y + (GlobalVariables.CELL_SIZE - 24) / 2, null);
                            break;
                        case 5: // 充电站
                            g2d.drawImage(chargingIcon, x + (GlobalVariables.CELL_SIZE - 24) / 2,
                                    y + (GlobalVariables.CELL_SIZE - 24) / 2, null);
                            break;
                        default:
                            // 什么都不绘制
                            break;
                    }
                }
            }

            // 绘制机器人
            double robotX = robotA.getY() * GlobalVariables.CELL_SIZE + 100;
            double robotY = robotA.getX() * GlobalVariables.CELL_SIZE + 100;
            if(!robotA.isCarryingProduct()) {
                if (robotA.getBattery() > 200) {
                    g2d.drawImage(robotAGIcon, (int) (robotX + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotAGIcon, (int) (robotX + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
                if (robotA.getBattery() <= 200 && robotA.getBattery() > 70) {
                    g2d.drawImage(robotAOIcon, (int) (robotX + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotAOIcon, (int) (robotX + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
                if (robotA.getBattery() <= 70) {
                    g2d.drawImage(robotARIcon, (int) (robotX + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotARIcon, (int) (robotX + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
            }
            else{
                if (robotA.getBattery() > 200) {
                    g2d.drawImage(robotAGcaryingIcon, (int) (robotX + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotAGcaryingIcon, (int) (robotX + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
                if (robotA.getBattery() <= 200 && robotA.getBattery() > 70) {
                    g2d.drawImage(robotAOcaryingIcon, (int) (robotX + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotAOcaryingIcon, (int) (robotX + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
                if (robotA.getBattery() <= 70) {
                    g2d.drawImage(robotARIcon, (int) (robotX + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotARIcon, (int) (robotX + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
            }
            double robotX2 = robotB.getY() * GlobalVariables.CELL_SIZE + 100;
            double robotY2 = robotB.getX() * GlobalVariables.CELL_SIZE + 100;
            if(!robotB.isCarryingProduct()) {
                if (robotB.getBattery() > 200) {
                    g2d.drawImage(robotBGIcon, (int) (robotX2 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY2 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotBGIcon, (int) (robotX2 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY2 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
                if (robotB.getBattery() <= 200 && robotB.getBattery() > 70) {
                    g2d.drawImage(robotBOIcon, (int) (robotX2 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY2 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotBOIcon, (int) (robotX2 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY2 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
                if (robotB.getBattery() <= 70) {
                    g2d.drawImage(robotBRIcon, (int) (robotX2 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY2 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotBRIcon, (int) (robotX2 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY2 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
            }
            else{
                if (robotB.getBattery() > 200) {
                    g2d.drawImage(robotBGcaryingIcon, (int) (robotX2 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY2 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotBGcaryingIcon, (int) (robotX2 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY2 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
                if (robotB.getBattery() <= 200 && robotB.getBattery() > 70) {
                    g2d.drawImage(robotBOcaryingIcon, (int) (robotX2 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY2 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotBOcaryingIcon, (int) (robotX2 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY2 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
                if (robotB.getBattery() <= 70) {
                    g2d.drawImage(robotBRIcon, (int) (robotX2 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY2 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotBRIcon, (int) (robotX2 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY2 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
            }

            double robotX3 = robotC.getY() * GlobalVariables.CELL_SIZE + 100;
            double robotY3 = robotC.getX() * GlobalVariables.CELL_SIZE + 100;
            if(!robotC.isCarryingProduct()) {
                if (robotC.getBattery() > 200) {
                    g2d.drawImage(robotCGIcon, (int) (robotX3 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY3 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotCGIcon, (int) (robotX3 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY3 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
                if (robotC.getBattery() <= 200 && robotC.getBattery() > 70) {
                    g2d.drawImage(robotCOIcon, (int) (robotX3 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY3 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotCOIcon, (int) (robotX3 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY3 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
                if (robotC.getBattery() <= 70) {
                    g2d.drawImage(robotCRIcon, (int) (robotX3 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY3 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotCRIcon, (int) (robotX3 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY3 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
            }
            else{
                if (robotC.getBattery() > 200) {
                    g2d.drawImage(robotCGcaryingIcon, (int) (robotX3 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY3 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotCGcaryingIcon, (int) (robotX3 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY3 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
                if (robotC.getBattery() <= 200 && robotC.getBattery() > 70) {
                    g2d.drawImage(robotCOcaryingIcon, (int) (robotX3 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY3 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotCOcaryingIcon, (int) (robotX3 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY3 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
                if (robotC.getBattery() <= 70) {
                    g2d.drawImage(robotCRIcon, (int) (robotX3 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY3 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                    g2d.drawImage(robotCRIcon, (int) (robotX3 + (GlobalVariables.CELL_SIZE - 24) / 2),
                            (int) (robotY3 + (GlobalVariables.CELL_SIZE - 24) / 2), null);
                }
            }
        }
    }

    // 工具方法：调整图像大小
//    private Image scaleImage(String imagePath, int width, int height) {
//        ImageIcon icon = new ImageIcon(imagePath);
//        return icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
//    }

    private Image scaleImage(String resourcePath, int width, int height) {
        InputStream is = getClass().getResourceAsStream(resourcePath);
        if (is == null) {
            throw new IllegalArgumentException("Resource not found: " + resourcePath);
        }
        BufferedImage originalImage = null;
        try {
            originalImage = ImageIO.read(is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Image scaledImage = originalImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        return scaledImage;
    }


    private boolean isShelfOccupied(int x, int y) {
        // 获取所有产品，检查是否有产品在 (x, y) 位置
        ProductsDAO productsDAO = new ProductsDAO();
        java.util.List<Products> products = productsDAO.getAllProducts();
        for (Products product : products) {
            if (product.getX() == x && product.getY() == y) {
                return true;  // 如果位置已被占用，返回 true
            }
        }
        return false;  // 如果没有产品占用，返回 false
    }

    private void updateRobotIcons() {
        System.out.println("Updating robot icons...");
        System.out.println("Robot A icon path: " + robotA.getIconPath());
        System.out.println("Robot B icon path: " + robotB.getIconPath());
        System.out.println("Robot C icon path: " + robotC.getIconPath());

       // robotAIcon = scaleImage(robotA.getIconPath(), 24, 24);
       // robotBIcon = scaleImage(robotB.getIconPath(), 24, 24);
        //robotCIcon = scaleImage(robotC.getIconPath(), 24, 24);

    }
}
   /* public static void main(String[] args) {
        // 初始化全局变量
        GlobalVariables.init();
        java.util.List<Robot> robotList = new ArrayList<>();
        robotList.add(new Robot("R1", 0, 0, RobotStatus.INACTIVE, 100, 0.1, 100, "src\\View\\images\\a-orange.png"));
        robotList.add(new Robot("R2", 5, 4, RobotStatus.INACTIVE, 100, 0.1, 100, "src\\View\\images\\b-green.png"));
        robotList.add(new Robot("R3", 10, 10, RobotStatus.INACTIVE, 100, 0.1, 100, "src\\View\\images\\c-red.png"));
        // 3. 创建SupervisorService
        SupervisorService supervisorService = new SupervisorService(robotList);
        // 启动窗口
        SwingUtilities.invokeLater(() -> {
            Warehouse frame = new Warehouse(supervisorService.getAllRobots());
            frame.setVisible(true);
        });
    }
}

*/